//
//  ProblemFeedBackViewController.m
//  FyApp
//
//  Created by user on 15/10/10.
//  Copyright © 2015年 user. All rights reserved.
//

#import "FeedbackViewController.h"
#import "sys/utsname.h"

@interface FeedbackViewController ()<UITextFieldDelegate,UITextViewDelegate>
{
    UITextView * _textView;
    UITextField *_nameLabelTF;
    
    UITextField *_contactTF;
    
    int keyboardhight;
    
    UIScrollView *_scrollView;
    
    UILabel *_placeholderLabel;
}
@property(nonatomic,strong)UIButton *sendBtn;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutViews];
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"意见反馈"];
    [self.view addSubview:topView];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    
    
    
    
}

- (void)layoutViews
{
    _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    [self.view addSubview:_scrollView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardhide)];
    tapGesture.cancelsTouchesInView = NO;
    [_scrollView addGestureRecognizer:tapGesture];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT*1.2);
    UILabel *sepLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 64.0f+9.5f, SCREEN_WIDTH, 0.5f)];
    sepLab.backgroundColor = [UIColor lightGrayColor];
    [_scrollView addSubview:sepLab];
    UILabel *sepLab1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 64.0f+10.0f+180.0f, SCREEN_WIDTH, 0.5f)];
    sepLab1.backgroundColor = [UIColor lightGrayColor];
    [_scrollView addSubview:sepLab1];

    //弹出留言框
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, 64.0f+10.0f, SCREEN_WIDTH, 180.0f)];
    _textView.font= [UIFont fontWithName:@"Arial"size:17.0];
    _textView.delegate= self;
    _textView.textColor = [UIColor hexStringToColor:@"#000000"];
    _textView.backgroundColor = [UIColor whiteColor];

    [_scrollView  addSubview:_textView];
    

    //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
    //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
    _placeholderLabel = [[UILabel alloc]init];
    _placeholderLabel.text = @"您的意见对我们很重要，在此输入您的宝贵意见";//#c2c2c2
    CGFloat height = [UILabel height:_placeholderLabel.text widthOfFatherView:SCREEN_WIDTH - 10.0f textFont:[UIFont fontWithName:@"Arial"size:17.0]];
    _placeholderLabel.frame =CGRectMake(5, 7, SCREEN_WIDTH-10.0f,height);
    _placeholderLabel.numberOfLines = 0;
    _placeholderLabel.textColor = [UIColor lightGrayColor];
    _placeholderLabel.alpha = 0.5;
    _placeholderLabel.font = [UIFont fontWithName:@"Arial"size:17.0];
    _placeholderLabel.enabled = NO;//lable必须设置为不可用
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    [_textView addSubview:_placeholderLabel];
    
    _nameLabelTF = [[UITextField alloc] initWithFrame:CGRectMake(X(_textView), NH(_textView)+16.0f, width(_textView), 48.0f)];
    _nameLabelTF.delegate = self;
    _nameLabelTF.backgroundColor = [UIColor whiteColor];
    _nameLabelTF.placeholder = @" 请留下您的手机号码或者邮箱";
    _nameLabelTF.keyboardType = UIKeyboardTypeDefault;
    _nameLabelTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nameLabelTF.tag = 1;
    _nameLabelTF.font = [UIFont systemFontOfSize:16.0f];
    [_scrollView addSubview:_nameLabelTF];
    
    
    
    if (!_sendBtn) {
        
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.frame  = CGRectMake(16.0f, NH(_nameLabelTF)+16.0f, SCREEN_WIDTH-32.0f, 44.0f);
        [_sendBtn setTitleColor:[UIColor hexStringToColor:@"#FFFFFF"] forState:UIControlStateNormal];
        _sendBtn.layer.cornerRadius = 2;
        [_sendBtn setBackgroundColor:[UIColor hexStringToColor:COLOR_VALUE] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:_sendBtn];
    }
    
    
    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 {
        // 不让输入表情
         if ([textView isFirstResponder]) {
        if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
                         return NO;
                     }
             }
    
         return YES;
     }

 // 过滤所有表情  https://gist.github.com/cihancimen/4146056
 - (BOOL)stringContainsEmoji:(NSString *)string {
         __block BOOL returnValue = NO;
         [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
                 ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                
                          const unichar hs = [substring characterAtIndex:0];
                          // surrogate pair
                          if (0xd800 <= hs && hs <= 0xdbff) {
                                 if (substring.length > 1) {
                                         const unichar ls = [substring characterAtIndex:1];
                                          const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                          if (0x1d000 <= uc && uc <= 0x1f77f) {
                                                  returnValue = YES;
                                              }
                                      }
                              } else if (substring.length > 1) {
                                      const unichar ls = [substring characterAtIndex:1];
                                      if (ls == 0x20e3) {
                                              returnValue = YES;
                                          }
                        
                                  } else {
                                         // non surrogate
                                       if (0x2100 <= hs && hs <= 0x27ff) {
                                                  returnValue = YES;
                                             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                               returnValue = YES;
                                                  } else if (0x2934 <= hs && hs <= 0x2935) {
                                                          returnValue = YES;
                                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                                              returnValue = YES;
                                                          } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                                                  returnValue = YES;
                                                              }
                                      }
                      }];
     
         return returnValue;
     }
//提交按钮
-(void)send:(UIButton*)btn
{
    //手机系统版本
    NSString* systemModel = [[UIDevice currentDevice] systemVersion];
    
    // 当前应用软件版本  比如：1.0.1
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appNumber = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    
    if ([_textView.text isEqualToString:@""]) {
        ALERT_VIEW(@"请输入您的意见");
        return;
    }
    if ([_nameLabelTF.text isEqualToString:@""]) {
        ALERT_VIEW(@"请输入您的联系方式");
        return;
    }
    if ([self stringContainsEmoji:_textView.text] == YES) {
        ALERT_VIEW(@"暂不支持表情输入");
        return;
    }
    if ([AccountTools validateEmail:_nameLabelTF.text]==NO&&[AccountTools validatePhoneNumber:_nameLabelTF.text]==NO ) {
        ALERT_VIEW(@"联系方式为邮箱，或者手机号");
        return;
    }
    
    
    if (_textView.text.length>200) {
        ALERT_VIEW(@"反馈意见最多只能输入200个字符");
        return;
    }
    
    if (_nameLabelTF.text.length>50) {
        ALERT_VIEW(@"联系方式最多只能输入50个字符");
        return;
    }
    
    [[AnimationIndicator sharedIndicator] startAnimation];
    
    [AppGlobalClass PostDataWithNSstring:INSERTFEEDBACK withDictionary:@{@"mobile":_nameLabelTF.text,@"content":_textView.text,@"mobileModel":[self deviceVersion],@"systemModel":systemModel,@"mobileType":@"1",@"appNumber":appNumber} success:^(NSDictionary *dictionary) {
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            
            ALERT_VIEW(@"意见反馈成功，我们将尽快为您处理");
            [self.navigationController popViewControllerAnimated:TRUE];
            
        }else
        {
            
//            NSString * str = [dictionary objectForKey:@"message"];
//            ALERT_VIEW(str);
            ALERT_VIEW(@"意见反馈成功，我们将尽快为您处理");
            [self.navigationController popViewControllerAnimated:TRUE];

            
        }
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"完成" withType:TRUE];
    } failure:^(NSError *error) {
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"失败" withType:TRUE];
    } networkerror:^(NSString *networkerror) {
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"无网络" withType:TRUE];
    }];
    
    
}

- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_contactTF resignFirstResponder];
    [_nameLabelTF resignFirstResponder];
    return YES;
}





-(void)textViewDidChange:(UITextView *)textView
{
    
    if (textView.text.length == 0) {
        _placeholderLabel.hidden = FALSE;
    }else{
        _placeholderLabel.hidden = TRUE;
    }
}

- (NSString*)deviceVersion
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([model isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([model isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([model isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([model isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([model isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([model isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([model isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([model isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([model isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([model isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([model isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([model isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([model isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([model isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([model isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([model isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([model isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([model isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([model isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([model isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([model isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([model isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([model isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([model isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([model isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([model isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([model isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([model isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([model isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([model isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([model isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([model isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([model isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([model isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([model isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([model isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([model isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([model isEqualToString:@"iPad4,4"])      return @"iPad Mini 2G (WiFi)";
    if ([model isEqualToString:@"iPad4,5"])      return @"iPad Mini 2G (Cellular)";
    if ([model isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
    if ([model isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (Cellular)";
    if ([model isEqualToString:@"iPad6,8"])      return @"iPad Pro";
    if ([model isEqualToString:@"i386"])         return @"Simulator";
    if ([model isEqualToString:@"x86_64"])       return @"Simulator";
    return model;
}
#pragma mark-键盘下落
-(void)keyboardhide
{
    [self.view endEditing:TRUE];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
